package com.github.thinwonton.springboot.showcase.web.exception.handle.exception.handler;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class ControllerExceptionAdvice {
    public static final String DEFAULT_ERROR_VIEW = "global_error";

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request,
                                            HttpServletResponse response,
                                            Exception e) throws Exception {
        System.out.println("================defaultErrorHandler==================");
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", request.getRequestURL());
        mav.setViewName(DEFAULT_ERROR_VIEW);
        return mav;
    }

//    @ExceptionHandler(value = Exception.class)
//    @ResponseBody
//    public JsonResult jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
//        return JsonResult.error(e.getMessage(), req.getRequestURL().toString());
//    }
}
